#if !USE_CONSOLE

#include "src/interface/objectDialog.h"

#include <QtGui>
#include <iostream>

#include "src/math/vector3d.h"

#include "src/defines/primitive.h"
#include "src/defines/material.h"
#include "src/defines/texture.h"

#include "src/scene/cuboidPrimitive.h"
#include "src/scene/spherePrimitive.h"
#include "src/scene/cylinderPrimitive.h"
#include "src/scene/conePrimitive.h"
#include "src/scene/texture.h"

ObjectDialog::ObjectDialog(int _id, unsigned int _type, GLWidget *_glWidget, QWidget *_parent)
    : MaterialDialog(_glWidget, _parent), id(_id), type(_type){

    switch(type)
    {
        case CUBOID:
            setWindowTitle(QString::fromUtf8("Novo cubóide"));
            break;
        case SPHERE:
            setWindowTitle(QString::fromUtf8("Nova esfera"));
            break;
        case CYLINDER:
            setWindowTitle(QString::fromUtf8("Novo cilindro"));
            break;
        case CONE:
            setWindowTitle(QString::fromUtf8("Novo cone"));
            break;
    }


    object = NULL;
    ambientR = diffuseR = specularR = specularG = specularB = 255;
    ambientG = ambientB = diffuseG = diffuseB = 0;
    ambientA = diffuseA = specularA = 255;
    specularPower = 80;
    ka = kd = ks = 0.5;
    kt = refractiveIndex = 0;

    textureFilePath = "texture/textura1.jpg";
    textureRepeatTimesU = textureRepeatTimesV = 5;
    textureColoringType = REPLACE;
    textureMappingType = ORIGINAL_TEXTURE;
    useTexture = false;

    bumpMappingFilePath = "texture/bumpmapping1.jpg";
    bumpMappingRepeatTimesU = bumpMappingRepeatTimesV = 5;
    bumpMappingMappingType = ORIGINAL_TEXTURE;
    bumpMappingIntensity = 0.1;
    bumpMappingLoop =  true;
    useBumpMapping = false;

    createLayout();
}

ObjectDialog::ObjectDialog(Object *_object, ObjectTreeItem *_item, GLWidget *_glWidget, QWidget *_parent)
    : MaterialDialog(_glWidget, _parent), object(_object), item(_item){

    setWindowTitle(QString::fromUtf8("Modificar objeto ").append(QString::number(_object->getId())));

    const Material &material = object->getMaterial();
    ambientR = material.ambient.red * 255;
    ambientG = material.ambient.green * 255;
    ambientB = material.ambient.blue * 255;
    ambientA = material.ambient.alpha * 255;

    diffuseR = material.diffuse.red * 255;
    diffuseG = material.diffuse.green * 255;
    diffuseB = material.diffuse.blue * 255;
    diffuseA = material.diffuse.alpha * 255;

    specularR = material.specular.red * 255;
    specularG = material.specular.green * 255;
    specularB = material.specular.blue * 255;
    specularA = material.specular.alpha * 255;

    specularPower = material.specularPower;
    ka = material.ka;
    kd = material.kd;
    ks = material.ks;
    kt = material.kt;
    refractiveIndex = material.refractiveIndex;

    type = object->getType();

    Texture *texture = material.texture;
    if(texture)
    {
        textureFilePath = texture->textureImagePathName;
        textureRepeatTimesU = texture->repeatTimesU;
        textureRepeatTimesV = texture->repeatTimesV;
        textureColoringType = texture->coloringType;
        textureMappingType = texture->mappingType;
        useTexture = true;
    }
    else{
        textureFilePath = "";
        textureRepeatTimesU = textureRepeatTimesV = 5;
        textureColoringType = MULTIPLY;
        textureMappingType = ORIGINAL_TEXTURE;
        useTexture = false;
    }

    BumpMapping *bumpMapping = material.bumpMapping;
    if(bumpMapping)
    {
        bumpMappingFilePath = bumpMapping->bumpMappingImagePathName;
        bumpMappingRepeatTimesU = bumpMapping->repeatTimesU;
        bumpMappingRepeatTimesV = bumpMapping->repeatTimesV;
        bumpMappingMappingType = bumpMapping->mappingType;
        bumpMappingIntensity = bumpMapping->bumpIntensity;
        bumpMappingLoop =  bumpMapping->loop;
        useBumpMapping = true;
    }
    else{
        bumpMappingFilePath = "";
        bumpMappingRepeatTimesU = bumpMappingRepeatTimesV = 5;
        bumpMappingMappingType = ORIGINAL_TEXTURE;
        bumpMappingIntensity = 0.1;
        bumpMappingLoop =  true;
        useBumpMapping = false;
    }

    createLayout();
}

ObjectDialog::~ObjectDialog(){
}

void ObjectDialog::createLayout(){

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndCreate()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QVBoxLayout *mainLayout = new QVBoxLayout;

    createMaterialLayout(mainLayout);

    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void ObjectDialog::acceptAndCreate(){
    Texture *texture = NULL;
    if(useTexture)
    {
        texture = new Texture(glWidget, textureFilePath,
                              textureRepeatTimesU, textureRepeatTimesV,
                              textureColoringType, textureMappingType);
    }
    BumpMapping *bumpMapping = NULL;
    if(useBumpMapping)
    {
        bumpMapping = new BumpMapping(bumpMappingFilePath,
                                      bumpMappingRepeatTimesU, bumpMappingRepeatTimesV,
                                      bumpMappingMappingType, bumpMappingIntensity, bumpMappingLoop);
    }

    if(!object){
        switch(type)
        {
            case CUBOID:
                object = new CuboidPrimitive(Material(Color(ambientR, ambientG, ambientB, ambientA)/255.0,
                                                      Color(diffuseR, diffuseG, diffuseB, diffuseA)/255.0,
                                                      Color(specularR, specularG, specularB, specularA)/255.0,
                                                      specularPower, ka, kd, ks, kt, refractiveIndex, texture, bumpMapping
                                                      ),
                                             id);
                break;
            case SPHERE:
                object = new SpherePrimitive(Material(Color(ambientR, ambientG, ambientB, ambientA)/255.0,
                                                      Color(diffuseR, diffuseG, diffuseB, diffuseA)/255.0,
                                                      Color(specularR, specularG, specularB, specularA)/255.0,
                                                      specularPower, ka, kd, ks, kt, refractiveIndex, texture, bumpMapping
                                                      ),
                                             id);
                break;
            case CYLINDER:
                object = new CylinderPrimitive(Material(Color(ambientR, ambientG, ambientB, ambientA)/255.0,
                                                        Color(diffuseR, diffuseG, diffuseB, diffuseA)/255.0,
                                                        Color(specularR, specularG, specularB, specularA)/255.0,
                                                        specularPower, ka, kd, ks, kt, refractiveIndex, texture, bumpMapping
                                                        ),
                                               id);
                break;
            case CONE:
                object = new ConePrimitive(Material(Color(ambientR, ambientG, ambientB, ambientA)/255.0,
                                                    Color(diffuseR, diffuseG, diffuseB, diffuseA)/255.0,
                                                    Color(specularR, specularG, specularB, specularA)/255.0,
                                                    specularPower, ka, kd, ks, kt, refractiveIndex, texture, bumpMapping
                                                    ),
                                           id);
                break;
        }

        done(object);
    }else{
        object->setMaterial(Material(Color(ambientR, ambientG, ambientB, ambientA)/255.0,
                                     Color(diffuseR, diffuseG, diffuseB, diffuseA)/255.0,
                                     Color(specularR, specularG, specularB, specularA)/255.0,
                                     specularPower, ka, kd, ks, kt, refractiveIndex, texture, bumpMapping
                                     )
                            );
    }

    accept();
    done();
}
#endif //!USE_CONSOLE
